package com.heima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.reggie.bean.Employee;
import com.heima.reggie.bean.PageParam;
import com.heima.reggie.mapper.EmployeeMapper;
import com.heima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper mapper;
    /**
      * 根据账号密码查询用户
      * @param
      * @param
      * @return
      */
    @Override
    public Employee login(Employee employee) {
        //1、根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername, employee.getUsername());
        //2、将页面提交的密码password进行md5加密处理
        String password=employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        wrapper.eq(Employee::getPassword, password);
        return mapper.selectOne(wrapper);
    }
/**
  * 新增
  * @param
  * @param
  * @return
  */
    @Override
    public int add(Employee employee) {
        return mapper.insert(employee);
    }
/**
  * 分页查询
  * @param
  * @param
  * @return
  */
    @Override
    public IPage<Employee> selectByPageAndCondition(PageParam pageParam) {
        QueryWrapper<Employee> qw = new QueryWrapper<>();
        qw.lambda().like(pageParam.getName()!=null,Employee::getName,pageParam.getName());
        IPage<Employee> Page = new Page<>(pageParam.getPage(),pageParam.getPageSize());
        return mapper.selectPage(Page,qw);
    }
/**
  * 更新
  * @param
  * @param
  * @return
  */
    @Override
    public int update(Employee employee) {
        return mapper.updateById(employee);
    }

    /*@Override
    public Employee selectById(Long id) {
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper();
        lqw.eq(Employee::getId, id);
        return mapper.selectOne(lqw);
    }*/
}
